﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DALBase;
using System.Data;
using System.Collections;
using MobileAPI.Common;
using MobileAPI.Entity.Model;

namespace MobileAPI.DAL
{
    public class DALLogin : BaseDAL
    {
        public DBAccess db = DBConnection.CreateDB();

        public bool IsUserExit(string LoginName)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("SELECT COUNT(1) FROM P_UserInfo WHERE Flag=1 AND LoginName=@LoginName");
                Hashtable hs = new Hashtable();
                hs.Add("LoginName", LoginName);
                int num = Convert.ToInt32(db.ExecuteScalar(sb.ToString(), hs));
                if (num == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                throw e;
            }
        }

        public EUser UserLogin(string name, string pwd)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("SELECT ");
                sb.AppendLine("[UserID],");
                sb.AppendLine("[LoginName],");
                sb.AppendLine("[UserName],");
                sb.AppendLine("[Password],");
                sb.AppendLine("[Description],");
                sb.AppendLine("[UserParam],");
                sb.AppendLine("[UserSite] ");
                sb.AppendLine("FROM P_UserInfo WHERE Flag=1 AND LoginName=@LoginName AND Password=@Password ");
                Hashtable hs = new Hashtable();
                hs.Add("LoginName", name);
                hs.Add("Password", pwd);
                DataTable dt = db.ExecuteQuery(sb.ToString(), hs);
                if (dt.Rows.Count > 0)
                {

                    EUser user = new EUser
                    {
                        UserID = dt.Rows[0]["UserID"].TInt32(),
                        LoginName = dt.Rows[0]["LoginName"].TString(),
                        Password = dt.Rows[0]["Password"].TString(),
                        UserName = dt.Rows[0]["UserName"].TString(),
                        Description = dt.Rows[0]["Description"].TString(),
                        UserParam = dt.Rows[0]["UserParam"].TString(),
                        UserSite = dt.Rows[0]["UserSite"].TString()
                    };
                    return user;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                base.WriteLog(e.Message);
                return null;
            }
        }
    }
}
